package xkp.lesson.consumer.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import xkp.lesson.consumer.entity.MQMessage;
import xkp.lesson.consumer.entity.StudentInfo;
import xkp.lesson.consumer.service.RocketMqService;
import xkp.lesson.consumer.websocket.ReceiptWebSocket;

import javax.annotation.Resource;
import java.io.IOException;


@RestController
public class ConsumerController {
    @Resource
    private RocketMqService rocketMqService;

    @Autowired
    private ReceiptWebSocket receiptWebSocket;

    @GetMapping("/testSss")
    public String testSss(){
        System.out.println("1111111111111");
        return rocketMqService.listDept();
    }

    @StreamListener(Sink.INPUT)
    public void input(Message<String> message) throws IOException, InterruptedException {

        MQMessage<StudentInfo> msg =
                JSON.parseObject(message.getPayload(), new TypeReference< MQMessage<StudentInfo>>() {});
        //System.out.println("接收到的消息： "+message.getPayload());
        String token = msg.getToken();
        Thread.sleep(5*1000);
        receiptWebSocket.sendMessageTo("true",token);
    }

}
